﻿(function () {
    "use strict";

    var list = new WinJS.Binding.List();
    var groupedItems = list.createGrouped(
        function groupKeySelector(item) { return item.group.key; },
        function groupDataSelector(item) { return item.group; }
    );

    // TODO: Replace the data with your real data.
    // You can add data from asynchronous sources whenever it becomes available.
    generateSampleData().then(
        function (items) {
            for (var i = 0; i < items.length; i++) {
                list.push(items[i]);
            }
        }, function (error) {
            console.log(error);
        });

    WinJS.Namespace.define("Data", {
        items: groupedItems,
        groups: groupedItems.groups,
        getItemReference: getItemReference,
        getItemsFromGroup: getItemsFromGroup,
        resolveGroupReference: resolveGroupReference,
        resolveItemReference: resolveItemReference
    });

    // Get a reference for an item, using the group key and item title as a
    // unique reference to the item that can be easily serialized.
    function getItemReference(item) {
        return [item.group.key, item.title];
    }

    // This function returns a WinJS.Binding.List containing only the items
    // that belong to the provided group.
    function getItemsFromGroup(group) {
        return list.createFiltered(function (item) { return item.group.key === group.key; });
    }

    // Get the unique group corresponding to the provided group key.
    function resolveGroupReference(key) {
        for (var i = 0; i < groupedItems.groups.length; i++) {
            if (groupedItems.groups.getAt(i).key === key) {
                return groupedItems.groups.getAt(i);
            }
        }
    }

    // Get a unique item from the provided string array, which should contain a
    // group key and an item title.
    function resolveItemReference(reference) {
        for (var i = 0; i < groupedItems.length; i++) {
            var item = groupedItems.getAt(i);
            if (item.group.key === reference[0] && item.title === reference[1]) {
                return item;
            }
        }
    }

    // Returns an array of sample data that can be added to the application's
    // data list. 
    function generateSampleData() {
        // These three strings encode placeholder images. You will want to set the
        // backgroundImage property in your real data to be URLs to images.
        var darkGray = "";
        var lightGray = "";
        var mediumGray = "";

        // Each of these sample groups must have a unique key to be displayed
        // separately.
        //{ key: "group6", title: "Group Title: 6", subtitle: "Group Subtitle: 6", backgroundImage: darkGray, description: groupDescription }
        //{ group: sampleGroups[0], title: "Item Title: 1", subtitle: "Item Subtitle: 1", description: itemDescription, content: itemContent, backgroundImage: lightGray }
        return new WinJS.Promise(function(complete, err, prog) {
            var sampleGroups = [];

            WinJS.xhr({
                type: "GET",
                url: "http://100nationalplaces.apphb.com/api/towns/getTownsAndPlaces",
                headers: { "Content-type": "application/json" },
            }).done(
               function(success) {
                    var allGroups = JSON.parse(success.responseText);
                    for (var k = 0; k < allGroups.length; k++) {
                        sampleGroups.push({ key: allGroups[k].Id, title: allGroups[k].TownName, subtitle: "", backgroundImage: darkGray, description: "" });
                    }
                    var sampleItems = [];
                    for (var i = 0; i < sampleGroups.length; i++) {
                        for (var j = 0; j < allGroups[i].Places.length; j++) {
                            sampleItems.push({ group: sampleGroups[i], title: allGroups[i].Places[j].Name, description: allGroups[i].Places[j].Description, backgroundImage: allGroups[i].Places[j].PictureUrl, subtitle: "", content: allGroups[i].Places[j].Description, });
                        }
                    }
                   
                    complete(sampleItems);
                },
                function(error) {
                    err(error);
                });
           
        });
    }
})();
